﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Entities.Modules;


namespace HyperCrunch.Druid.Mobile
{
    public class ModuleMetaInfo
    {
        public string ModuleFolder { get; set; }
        public string MobileFolder { get; set; }
        public string MobileUrl { get; set; }
        public string AssemblyNamespace { get; set; }
        public string AssemblyFolder { get; set; }
        public string AssemblyFile { get; set; }
        public string AppViewFile { get; set; }
        public string AppControllerFile { get; set; }
        public string AppModelFile { get; set; }
        public string AppScriptFile { get; set; }
        public string AppStylesheetFile { get; set; }
        public string AssemblyInfoFile { get; set; }
        public string LogFile { get; set; }

        public static ModuleMetaInfo Instance(ModuleInfo moduleInfo, UIContext uiContext)
        {
            string moduleFolder = moduleInfo.ModuleControl.ControlSrc.Substring(0, moduleInfo.ModuleControl.ControlSrc.LastIndexOf("/"));
            string mobileFolder = uiContext.Context.Server.MapPath("~/" + moduleFolder + "/" + Globals.RAZOR_MOBILE_FOLDER + "/").Replace("//", "/");
            string mobileUrl = (uiContext.Context.Request.ApplicationPath + "/" + moduleFolder + "/" + Globals.RAZOR_MOBILE_FOLDER + "/").Replace("//","/");
            string assemblyNamespace = GetModuleAssemblyName(moduleInfo);
            if (String.IsNullOrEmpty(assemblyNamespace)) assemblyNamespace = moduleInfo.DesktopModule.ModuleName;
            string assemblyFolder = uiContext.Context.Server.MapPath("~/bin/Modules/");
            string assemblyFile = assemblyFolder + assemblyNamespace + ".Mobile.dll";
            string appViewFile = mobileFolder + Globals.RAZOR_MOBILE_APP_VIEW;
            string appControllerFile = mobileFolder + Globals.RAZOR_MOBILE_APP_CONTROLLER;
            string appModelFile = mobileFolder + Globals.RAZOR_MOBILE_APP_MODEL;
            string appScriptFile = mobileFolder + Globals.RAZOR_MOBILE_APP_SCRIPT;
            string appStylesheetFile = mobileFolder + Globals.RAZOR_MOBILE_APP_STYLESHEET;
            string assemblyInfoFile = mobileFolder + Globals.RAZOR_MOBILE_ASSEMBLYINFO;
            string logFile = mobileFolder + Globals.RAZOR_MOBILE_LOG;
            return new ModuleMetaInfo
            {
                ModuleFolder = moduleFolder,
                MobileFolder = mobileFolder,
                MobileUrl = mobileUrl,
                AssemblyNamespace = assemblyNamespace,
                AssemblyFolder = assemblyFolder,
                AssemblyFile = assemblyFile,
                AppViewFile = appViewFile,
                AppControllerFile = appControllerFile,
                AppModelFile = appModelFile,
                AppScriptFile = appScriptFile,
                AppStylesheetFile = appStylesheetFile,
                AssemblyInfoFile = assemblyInfoFile,
                LogFile = logFile
            };
        }

        private static string GetModuleAssemblyName(ModuleInfo moduleInfo)
        {
            string bizController = moduleInfo.DesktopModule.BusinessControllerClass;
            if (String.IsNullOrEmpty(bizController) || (bizController.IndexOf(',') < 0)) return null;
            return bizController.Split(',')[1].Trim();
        }
    }
}
